"""
Helper functions that convert strftime formats into more readable representations.

将 strftime 格式转换为更易读的表示形式的辅助函数
"""

from my_typing import *
from utils.serializer_utils import settings


def datetime_formats(formats: Iterable[str]) -> str:
    """ 日期时间 格式 """

    fmt: str = "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"  # noqa

    return _replace_formats(formats, fmt)


def date_formats(formats: Iterable[str]) -> str:
    """ 日期 格式 """

    fmt: str = "YYYY-MM-DD"

    return _replace_formats(formats, fmt)


def time_formats(formats: Iterable[str]) -> str:
    """ 时间 格式 """

    fmt: str = "hh:mm[:ss[.uuuuuu]]",  # noqa

    return _replace_formats(formats, fmt)


def _replace_formats(formats: Iterable[str], fmt: str) -> str:
    """ 替换格式 """

    format_string = ", ".join(formats).replace(settings.ISO_8601, fmt)

    return humanize_strptime(format_string)


def humanize_strptime(format_string: str) -> str:  # noqa
    """
    人性化的 strptime

    - Note that we"re missing some of the locale specific mappings that don"t really make sense.
    - 请注意，我们缺少一些特定区域设置的映射，这些映射实际上并不合理

    Args:
        format_string: strftime 格式
    """

    mapping: DictStrStr = {
        "%Y": "YYYY",
        "%y": "YY",
        "%m": "MM",
        "%b": "[Jan-Dec]",
        "%B": "[January-December]",
        "%d": "DD",
        "%H": "hh",
        "%I": "hh",  # Requires "%p" to differentiate from "%H".
        "%M": "mm",
        "%S": "ss",
        "%f": "uuuuuu",  # noqa
        "%a": "[Mon-Sun]",
        "%A": "[Monday-Sunday]",
        "%p": "[AM|PM]",
        "%z": "[+HHMM|-HHMM]"
    }

    for key, val in mapping.items():
        format_string = format_string.replace(key, val)

    return format_string
